package com.llp.javaweb.servlet;

import com.llp.javaweb.service.SetEmailService;
import com.llp.javaweb.service.impl.SetEmailServiceImpl;
import com.llp.javaweb.util.CheckIsLogin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author llp
 * {@code @date} 2022年11月02日16:30
 */
@WebServlet("/mail/setMail")
public class setMailServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //检验是否登录
        boolean b = CheckIsLogin.IsLogin(req, resp);
        if (!b)
            resp.sendRedirect("/setEmailError.html");
        else
        req.getRequestDispatcher("/WEB-INF/templates/product/mailmanager.html").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //检验是否登录
        boolean b = CheckIsLogin.IsLogin(req, resp);
        if (!b)
            resp.sendRedirect("/setEmailError.html");
        else {
            //获取Email对象
            String  fromaddressname=req.getParameter("fromaddressname");
            String fromaddresstype=req.getParameter("fromaddresstype");
            String fromaddress=fromaddressname+fromaddresstype;
            String frompassword=req.getParameter("frompassword");
            String toaddress=req.getParameter("toaddress");

            //更新Email信息
            SetEmailService setEmailService=new SetEmailServiceImpl();
            setEmailService.setEmail(fromaddress,frompassword,toaddress);
            log("修改成功");
            resp.sendRedirect("/mail/setMail");

        }



    }
}
